package com.wdz.controller;

import cn.hutool.http.HttpUtil;
import cn.hutool.json.JSONObject;
import com.alibaba.fastjson.JSON;
import com.wdz.ArticleApplication;
import com.wdz.annotations.ResponseResult;
import com.wdz.http.Response;
import com.wdz.redis.ApplicationContextUtil;
import com.wdz.redis.RedisUtil;
import com.wdz.utils.RedisFieldName;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/index")
public class IndexController {
    @Autowired
    private RedisUtil redisUtil;
    @GetMapping("/")
    public Response get(){
        //RedisUtil redisUtil = (RedisUtil) ApplicationContextUtil.getBean("RedisUtil");
        String rn = RedisFieldName.INDEXDASHBOARD;
        Map<Object,Object> data = redisUtil.hmget(rn);
        if(data.isEmpty()){
            ArticleApplication articleApplication = (ArticleApplication) ApplicationContextUtil.getBean("ArticleApplication");
            articleApplication.setIndexDash();
            data = redisUtil.hmget(rn);
        }
        //转换json
        //JSONObject jsonObject = JSONUtil.parseObj(data.get("7tq"));
        //Set<String> key = jsonObject.keySet();
       // key.forEach(item->{
            //jsonObject.put(item,jsonObject.getStr(item).substring(1,jsonObject.getStr(item).length()-1));
        //});
        //data.put("7tq", jsonObject);
        return Response.success(data);
    }
    //爬取lol资讯
    @ResponseResult
    @GetMapping("/getLols/{pageNo}/{pageSize}")
    public Map<String, Object> getLols(@PathVariable Integer pageNo, @PathVariable Integer pageSize){
        String result1= HttpUtil.get("https://apps.game.qq.com/cmc/cross?serviceId=3&source=gw_101glzx&tagids=1911,1286&typeids=1,2&limit="+pageSize+"&start="+pageNo+"0&logic=and");
        JSONObject jsonObject = JSON.parseObject(result1,JSONObject.class);
        jsonObject = jsonObject.get("data", JSONObject.class);
        Map<String,Object> d = new HashMap<>();
        d.put("total",jsonObject.get("total"));
        d.put("data",jsonObject.get("items"));
        return d;
    }

    /**
     * 获取IP地址
     * @return 返回地址 ip
     */
    @ResponseResult
    @GetMapping("/getIp")
    public Map<String,Object> getIp(){
        String result1= HttpUtil.get("https://www.fkcoder.com/myip");
        String result2= HttpUtil.get("https://www.fkcoder.com/ip?ip="+result1);
        Map<String,Object> data = JSON.parseObject(result2,Map.class);
        data.put("ip",result1);
        return data;
    }

}
